package com.central.apps.controller;

import com.central.app.service.HandlerService;
import com.central.bizcommon.annotation.log.BussinessLog;
import com.central.bizcommon.log.LogType;
import com.mchange.lang.DoubleUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/echart")
public class EchartInfos {

    @Autowired private HandlerService handlerService;

    //近五年资产总额(亿元)：
    @GetMapping("/getAssetPrices")
    @BussinessLog(module = "apps-center", value = "企业画像应用接口:近五年资产总额(亿元)")
    public Map<String, Object> getAssetPrices(){
        //获取当前日期年份以及近5年年份,作为where查询条件
        Map<String, Object> assetPrices =new HashMap();
        List<String> yearLists =new ArrayList<>();
        List<String> priceLists =new ArrayList<>();
        Calendar calendar =Calendar.getInstance();
        int year =calendar.get(Calendar.YEAR);
        int years [] =new int[]{year-4,year-3,year-2,year-1,year};
        //查询每年资产总额
        for(int i : years){
            yearLists.add(Integer.toString(i));
            String getAssetPrices ="select sum(originalvalue) as prices from gq_assethead where YEAR(createtime)="+i;
            List<Map<String, Object>> getAssetPricesbySql = handlerService.findBySql(getAssetPrices);
            if(MapUtils.getString(getAssetPricesbySql.get(0),"prices")==null || "".equals(MapUtils.getString(getAssetPricesbySql.get(0),"prices"))){
                priceLists.add("0");
            }else {
                double prices = (double) getAssetPricesbySql.get(0).get("prices");
                prices=prices/100000000;
                priceLists.add(Double.toString(prices));
            }
        }
        assetPrices.put("categoryData",yearLists);
        assetPrices.put("seriesData",priceLists);
        return assetPrices;
    }

    //近5年入账数量
    @GetMapping("/getAssetNums")
    @BussinessLog(module = "apps-center", value = "企业画像应用接口:近5年入账数量")
    public Map<String, Object> getAssetNums(){
        //获取当前日期年份以及近5年年份,作为where查询条件
        Map<String, Object> assetNums =new HashMap();
        List<String> yearLists =new ArrayList<>();
        List<String> numLists =new ArrayList<>();
        Calendar calendar =Calendar.getInstance();
        int year =calendar.get(Calendar.YEAR);
        int years [] =new int[]{year-4,year-3,year-2,year-1,year};
        for(int i : years){
            yearLists.add(Integer.toString(i));
            String getAssetNums ="select count(*) as nums from gq_assethead where YEAR(createtime)="+i;
            List<Map<String, Object>> getAssetNumsbySql = handlerService.findBySql(getAssetNums);
            if(MapUtils.getString(getAssetNumsbySql.get(0),"nums")==null || "".equals(MapUtils.getString(getAssetNumsbySql.get(0),"nums"))){
                numLists.add("0");
            }else {
                long prices = (long) getAssetNumsbySql.get(0).get("nums");
                numLists.add(Long.toString(prices));
            }
        }
        assetNums.put("categoryData",yearLists);
        assetNums.put("seriesData",numLists);
        return assetNums;
    }

    //分类排名统计Top10
    @GetMapping("/getTopTen")
    @BussinessLog(module = "apps-center", value = "企业画像应用接口:分类排名统计Top10")
    public Map<String, Object> getTopTen(){
        Map<String, Object> getTopTen =new HashMap<>();
        List<String> nameLists =new ArrayList<>();
        List<String> dateLists =new ArrayList<>();
        String sql ="select classifydescribe,count(id) as nums from gq_assethead group by classifydescribe;";
        List<Map<String, Object>> bySql = handlerService.findBySql(sql);
        for(Map<String, Object> map :bySql){
            String classifydescribe ="其他";
            if(!(MapUtils.getString(map,"classifydescribe") == null) && !"".equals(MapUtils.getString(map,"classifydescribe"))){
                classifydescribe= (String) map.get("classifydescribe");
            }
            nameLists.add(classifydescribe);
            long nums = (long) map.get("nums");
            dateLists.add(Long.toString(nums));
        }
        getTopTen.put("categoryData",nameLists);
        getTopTen.put("seriesData",dateLists);
        return getTopTen;
    }

    //资产维修费用占比
    @GetMapping("/assetFixbili")
    @BussinessLog(module = "apps-center", value = "企业画像应用接口:资产维修费用占比")
    public List<Map<String, Object>> assetFixbili(){
        List<Map<String, Object>> assetFix1 =new ArrayList<>();
        /*String sql ="select status,sum(fixcosts) as fixcosts from gq_workorder where UDWOTYPE='检维修工单' GROUP BY status;";
        List<Map<String, Object>> bySql = handlerService.findBySql(sql);
        for(Map<String, Object> map :bySql){
            Map<String, Object> map1 =new HashMap<>();
            String status= (String) map.get("status");
            map1.put("name",status);
            double nums =0;
            if(!(MapUtils.getString(map,"fixcosts") == null) && !("".equals(MapUtils.getString(map,"fixcosts")))){
                nums = (double) map.get("fixcosts");
            }
            map1.put("value",nums);
            assetFix1.add(map1);
        }*/
        return assetFix1;
    }

    //资产金额按部门占比
    @GetMapping("/assetPrics")
    @BussinessLog(module = "apps-center", value = "企业画像应用接口:资产金额按部门占比")
    public List<Map<String, Object>> assetPrics(){
        List<Map<String, Object>> assetPrics1 =new ArrayList<>();
        String sql ="select deptcode,sum(purchaseprice)as prices from gq_assethead group by deptcode;";
        List<Map<String, Object>> bySql = handlerService.findBySql(sql);
        for(Map<String, Object> map :bySql){
            Map<String, Object> map1 =new HashMap<>();
            String deptcode= (String) map.get("deptcode");
            map1.put("name",deptcode);
            double prices = 0;
            if(!(MapUtils.getString(map,"prices") == null) && !("".equals(MapUtils.getString(map,"prices")))){
                prices = (double) map.get("prices");
            }
            map1.put("value",prices);
            assetPrics1.add(map1);
        }
        return assetPrics1;
    }

    //资产维修费用占比
    @GetMapping("/assetFix")
    @BussinessLog(module = "apps-center", value = "企业画像应用接口:资产维修费用统计")
    public Map<String, Object> assetFix(){
        Map<String, Object> assetFix1 =new HashMap<>();
       /* String sql ="select status,sum(fixcosts) as fixcosts from gq_workorder where UDWOTYPE='检维修工单' GROUP BY status;";
        List<Map<String, Object>> bySql = handlerService.findBySql(sql);
        List<String> nameLists =new ArrayList<>();
        List<String> dateLists =new ArrayList<>();
        for(Map<String, Object> map :bySql){
            String status= (String) map.get("status");
            nameLists.add(status);
            double nums =0;
            if(!(MapUtils.getString(map,"fixcosts") == null) && !("".equals(MapUtils.getString(map,"fixcosts")))){
                nums = (double) map.get("fixcosts");
            }
            dateLists.add(Double.toString(nums));
        }
        assetFix1.put("name","资产维修费用统计");
        assetFix1.put("categoryData",nameLists);
        assetFix1.put("seriesData",dateLists);*/
        return assetFix1;
    }

    //获取工单信息
    @GetMapping("/getWorkorder")
    @BussinessLog(module = "apps-center", value = "企业画像应用接口:获取工单信息")
    public Map<String, Object> getWorkorder(){
        Map<String, Object> map =new HashMap<>();
        //本月工单数量
        String sql ="select count(id) as nums from gq_workorder where date_format(createtime,'%Y-%m')=date_format(now(),'%Y-%m');";
        List<Map<String, Object>> bySql = handlerService.findBySql(sql);
        String nums ="0";
        if(!(MapUtils.getString(bySql.get(0),"nums") == null) && !("".equals(MapUtils.getString(bySql.get(0),"nums")))){
            nums =MapUtils.getString(bySql.get(0),"nums");
        }
        map.put("a", nums);
        //在执行数量
        String nums1 ="0";
        String sql2 ="select count(id) as nums  from gq_workorder where status ='待审批' ;";
        List<Map<String, Object>> bySql2 = handlerService.findBySql(sql2);
        if(!(MapUtils.getString(bySql2.get(0),"nums") == null) && !("".equals(MapUtils.getString(bySql2.get(0),"nums")))){
            nums1 =MapUtils.getString(bySql2.get(0),"nums");
        }
        map.put("b",nums1);
        //已完成数量
        String nums2 ="0";
        String sql3 ="select count(id) as nums  from gq_workorder where status ='已审批';";
        List<Map<String, Object>> bySql3 = handlerService.findBySql(sql3);
        if(!(MapUtils.getString(bySql3.get(0),"nums") == null) && !("".equals(MapUtils.getString(bySql3.get(0),"nums")))){
            nums2 =MapUtils.getString(bySql3.get(0),"nums");
        }
        map.put("c", nums2);
        //本年完成数量
        String nums3 ="0";
        String sql4 ="select count(id) as nums  from gq_workorder where status ='已审批' and date_format(createtime,'%Y')=date_format(now(),'%Y');";
        List<Map<String, Object>> bySql4 = handlerService.findBySql(sql4);
        if(!(MapUtils.getString(bySql4.get(0),"nums") == null) && !("".equals(MapUtils.getString(bySql4.get(0),"nums")))){
            nums3 =MapUtils.getString(bySql4.get(0),"nums");
        }
        map.put("d",nums3);
        return map;
    }

    //获取企业价值
    @GetMapping("/getBusValue")
    @BussinessLog(module = "apps-center", value = "企业画像应用接口:获取企业价值")
    public Map<String, Object> getBusValue(){
        //资产台账原值字段综合
        String sql ="select sum(originalvalue)/100000000 as value from gq_assethead;";
        List<Map<String, Object>> bySql = handlerService.findBySql(sql);
        double value =0;
        if(!(MapUtils.getString(bySql.get(0),"value") == null) && !("".equals(MapUtils.getString(bySql.get(0),"value")))){
            value = (double) bySql.get(0).get("value");
        }
        String busvalue = String.format("%.2f", value);
        Map<String, Object> map =new HashMap<>();
        map.put("value",busvalue);
        return  map;
    }

    //获取企业总人数
    @GetMapping("/getBusPeoples")
    @BussinessLog(module = "apps-center", value = "企业画像应用接口:获取企业总人数")
    public Map<String, Object> getBusPeoples(){
        //用户表总数
        String sql ="select count(id) as peoples from sys_user";
        List<Map<String, Object>> bySql = handlerService.findBySql(sql);
        String peoples =MapUtils.getString(bySql.get(0),"peoples");
        Map<String, Object> map =new HashMap<>();
        map.put("peoples",peoples);
        return  map;
    }
}
